package com.lfy.yunpicture.manager;

import cn.hutool.core.io.FileUtil;
import com.lfy.yunpicture.constant.FileConstant;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.lfy.yunpicture.config.CosClientConfig;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * Cos 对象存储操作
 */
@Component
@Slf4j
public class CosManager {

    @Resource
    private CosClientConfig cosClientConfig;

    @Resource
    private COSClient cosClient;

    /**
     * 上传对象
     *
     * @param key           唯一键
     * @param localFilePath 本地文件路径
     * @return
     */
    public PutObjectResult putObject(String key, String localFilePath) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key,
                new File(localFilePath));
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 上传对象
     *
     * @param key  唯一键
     * @param file 文件
     * @return
     */
    public PutObjectResult putObject(String key, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key,
                file);
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 上传图片并返回基本信息
     */
    public PutObjectResult putPictureObject(String key, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key,
                file);
        // 对图片进行处理（获取基本信息也被视作为一种处理）
        PicOperations picOperations = new PicOperations();
        // 1 表示返回原图信息
        picOperations.setIsPicInfo(1);
        // 文件压缩，转成webp格式
        List<PicOperations.Rule> ruleList = new ArrayList<>();
        PicOperations.Rule rule = new PicOperations.Rule();
        // url后缀转换为webp格式
        int i = key.lastIndexOf(".");
        String webp = "/" + key.substring(0, i + 1) + "webp";
        rule.setFileId(webp);
        rule.setBucket(cosClientConfig.getBucket());
        rule.setRule("imageMogr2/format/webp");
        ruleList.add(rule);
        // 设置缩略图
        // 如果图片大小大于20KB，才进行缩略
        if (file.length() > 20 * 1024) {
            PicOperations.Rule rule1 = new PicOperations.Rule();
            String thumbnailKey = "/" + key.substring(0, i) + "_thumbnail." + FileUtil.getSuffix(key);
            rule1.setFileId(thumbnailKey);
            rule1.setBucket(cosClientConfig.getBucket());
            rule1.setRule("imageMogr2/thumbnail/200x200");
            // 构造处理参数
            ruleList.add(rule1);
        }
        // 构造处理参数
        picOperations.setRules(ruleList);
        putObjectRequest.setPicOperations(picOperations);
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 删除文件操作
     *
     * @param key
     */
    public void deleteObject(String key, String thumbnailKey) {
        if (key != null){
            key = key.substring(FileConstant.COS_HOST.length());
            cosClient.deleteObject(cosClientConfig.getBucket(), key);
            log.info("删除文件成功, 文件路径 = {}", key);
        }
        if (thumbnailKey != null){
            thumbnailKey = thumbnailKey.substring(FileConstant.COS_HOST.length());
            cosClient.deleteObject(cosClientConfig.getBucket(), thumbnailKey);
            log.info("删除文件成功, 文件路径 = {}", thumbnailKey);
        }
    }
}
